package com.aier.proxy.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.proxy.entity.ProxyCity;

public class ProxyCityRowMapper implements RowMapper<ProxyCity> {

    private int type;
    public static final int AND_CNAME = 1;
    public ProxyCityRowMapper() {
    }

    public ProxyCityRowMapper(int type) {
        this.type = type;
    }

    @Override
    public ProxyCity mapRow(ResultSet rs, int rowNum) throws SQLException {
        ProxyCity pc = null;
        switch (type) {
            case AND_CNAME:
                pc = parseProxyCity(new ProxyCity(), rs);
                pc.setCname(rs.getString("cname"));
                break;
            default:
                pc = parseProxyCity(new ProxyCity(), rs);
        }
        return pc;
        
    }

    public static ProxyCity parseProxyCity(ProxyCity pc, ResultSet rs) throws SQLException {
        return parseProxyCity(pc, rs, null);
    }

    public static ProxyCity parseProxyCity(ProxyCity pc, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            pc.setPid(rs.getInt(su+"pid"));
        } catch (SQLException e) {
            pc.setPid(null);
        }
        try {
            pc.setCid(rs.getInt(su+"cid"));
        } catch (SQLException e) {
            pc.setCid(null);
        }

        return pc;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, ProxyCity pc) {
        args.put("pid", pc.getPid());
        args.put("cid", pc.getCid());
        return args;
    }
    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final ProxyCity proxyCity) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(proxyCity),
                        new String[] { "pid","cid" });
                parsePS(ps, proxyCity);
                return ps;
            }

        };
    }

    private static String parseSQL(ProxyCity proxyCity) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into proxy_city(");
        int count = 0;
        if (null != proxyCity.getPid()) {
            sql.append("pid,");
            count++;
        }        
        if (null != proxyCity.getCid()) {
            sql.append("cid,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, ProxyCity proxyCity)
        throws SQLException {
        int count = 1;
        if (null != proxyCity.getPid()) {
            ps.setInt(count++, proxyCity.getPid());
        }        
        if (null != proxyCity.getCid()) {
            ps.setInt(count++, proxyCity.getCid());
        }

    }
    
    
    
    



}
